package com.alibaba.citrus.cr.mn.order.center.facade.function.impl;

import com.alibaba.citrus.cr.common.enums.ordercenter.*;
import com.alibaba.citrus.cr.mn.order.center.common.SetOrderTag;
import com.alibaba.citrus.cr.mn.order.center.common.util.StopWatchUtils;
import com.alibaba.citrus.cr.mn.order.center.facade.adapter.BaseDataServiceAdapter;
import com.alibaba.citrus.cr.mn.order.center.facade.function.check.AbstractOrderParamsCheckService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.check.ReturnOrderCreateParamsCheck;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.AbstractOrderAtomCreateService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.OrderAtomCreateService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.common.SendMsgService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.AbstractOrderParamsConvertService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.convert.ReturnOrderParamsConvertService;
import com.alibaba.citrus.cr.mn.order.center.facade.function.dataobject.AtomOrderDO;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crb2btradecenter.domain.order.model.Order;
import com.epoch.app.crb2btradecenter.domain.orderline.model.OrderLine;
import com.epoch.app.mnordercenter.model.dto.DictionaryResponse;
import com.epoch.app.mnordercenter.model.dto.ReturnOrderAtomCreateRequest;
import com.epoch.app.mnordercenter.orderbaserequest.OrderAtomCreateResponse;
import com.google.common.collect.Maps;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * Chen.mu
 * 2023/5/11
 **/
@Slf4j
@Service("returnOrderAtomCreateService")
public class ReturnOrderAtomCreateServiceImpl extends AbstractOrderAtomCreateService implements OrderAtomCreateService<ReturnOrderAtomCreateRequest, OrderAtomCreateResponse> {

    @Resource
    private ReturnOrderCreateParamsCheck returnOrderCreateParamsCheck;
    @Resource
    private ReturnOrderParamsConvertService returnOrderParamsConvertService;
    @Resource
    private SendMsgService sendMsgService;
    @Resource
    private BaseDataServiceAdapter baseDataServiceAdapter;
    @Override
    public AbstractOrderParamsCheckService<ReturnOrderAtomCreateRequest, OrderAtomCreateResponse> getCheckService() {
        return returnOrderCreateParamsCheck;
    }

    @Override
    public AbstractOrderParamsConvertService<ReturnOrderAtomCreateRequest, AtomOrderDO> getConvertService() {
        return returnOrderParamsConvertService;
    }
    @Override
    public List<AtomOrderDO> convert(List<ReturnOrderAtomCreateRequest> requestList) {
        assembleSalesOrderCreateCommonInfo(requestList);
        return returnOrderParamsConvertService.convert(requestList);
    }
    @Override
    public String getOrderTag() {
        return SetOrderTag.RETURN;
    }


    @Override
    public void afterSubmitTransactional(List<AtomOrderDO> orderWriteList) {
        log.info("ReturnOrderAtomCreateService#afterSubmitTransactional orderWriteList={}", JSON.toJSONString(orderWriteList));
        StopWatchUtils.start(CORE_STOP_WATCH_SEQ, "afterSubmitTransactional");

        //生成销售单对象<主单ID, List<子单ID>>
        Map<String, List<String>> confirmOrderMap = Maps.newHashMap();
        for (AtomOrderDO atomOrderDO : orderWriteList) {
            if (!atomOrderDO.isAutoSubmit()) {
                continue;
            }
            Order order = atomOrderDO.getOrder();
            List<OrderLine> orderLines = atomOrderDO.getOrderLines();
            confirmOrderMap.put(String.valueOf(order.getId()),
                    orderLines.stream().map(p -> String.valueOf(p.getId())).collect(Collectors.toList()));
        }

        // 发送SAP消息
        if (MapUtils.isNotEmpty(confirmOrderMap)) {
            sendMsgService.startTaskAndSendSalesOrderMessage(confirmOrderMap);
        }

        StopWatchUtils.stop();
    }

    public void assembleSalesOrderCreateCommonInfo(List<ReturnOrderAtomCreateRequest> returnOrderAtomCreateRequests) {

        List<DictionaryResponse> businessTypeResponseList = baseDataServiceAdapter.queryDictionarys(DictionaryTypeEnum.BUSINESS_TYPE.getCode());
        Map<String, DictionaryResponse> businessTypeMap = businessTypeResponseList.stream().collect(Collectors.toMap(DictionaryResponse::getCode, Function.identity(), (k1, k2) -> k2));
        for (ReturnOrderAtomCreateRequest bizCreateRequest : returnOrderAtomCreateRequests) {

            String salesOrderType = bizCreateRequest.getSalesOrderType();
            if (OrderCategoryEnum.GROUP.getCode().equals(bizCreateRequest.getOrderCategoryCode())){
                if (StringUtils.isNotBlank(salesOrderType)) {
                    ReturnOrderTypeEnum returnOrderTypeEnum = ReturnOrderTypeEnum.of(salesOrderType);
                    if (Objects.nonNull(returnOrderTypeEnum)) {
                        bizCreateRequest.setSalesOrderTypeName(returnOrderTypeEnum.getDesc());
                    }
                }
            }else {
                if (StringUtils.isNotBlank(salesOrderType)) {
                    SalesOrderTypeEnum salesOrderTypeEnum = SalesOrderTypeEnum.of(salesOrderType);
                    if (Objects.nonNull(salesOrderTypeEnum)) {
                        bizCreateRequest.setSalesOrderTypeName(salesOrderTypeEnum.getDesc());
                    }
                }
            }

            String deliveryMethodCode = bizCreateRequest.getDeliveryMethodCode();
            if (StringUtils.isNotBlank(deliveryMethodCode)) {
                SalesOrderDeliveryMethodEnum deliveryMethodEnum = SalesOrderDeliveryMethodEnum.of(deliveryMethodCode);
                if (Objects.nonNull(deliveryMethodEnum)) {
                    bizCreateRequest.setDeliveryMethodName(deliveryMethodEnum.getDesc());
                }
            }

            String businessType = bizCreateRequest.getBusinessType();
            if (StringUtils.isNotBlank(businessType)) {
                DictionaryResponse dictionaryResponse = businessTypeMap.get(businessType);
                if (Objects.nonNull(dictionaryResponse)) {
                    bizCreateRequest.setBusinessTypeName(dictionaryResponse.getName());
                }
            }
        }
    }

}
